<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Ad extends Controller
{
    private $_model;
    private $_mapper;
    private $title_field = [
    ];
    public $pagesize = 10;
    public $optypeLists = array(
        '0' => '无',
        '1' => '链接',
        '2' => '功能编号',
    );

    public $positionLists = array(
        '0' => '无',
        '10' => '[首页][顶部广告]',
    );

    public function _initialize()
    {
        (new \app\admin\behavior\CheckPermissions())->run();//增加权限控制
        $this->_model = new \app\admin\model\Ad();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
        $this->assign('optypeLists', $this->optypeLists);
        $this->assign('positionLists', $this->positionLists);
        $this->_mapper = \app\admin\mapper\Ad::getInstance();
        $this->_mapper->setOptypeLists($this->optypeLists);
        $this->_mapper->setPositionLists($this->positionLists);
    }

    public function shangData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 0), $id);
        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }

    public function xiaData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 1), $id);

        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }

    public function lists()
    {
        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $ser_stats = trim(input("request.ser_stats"));//auth
        $ser_position = trim(input("request.ser_position"));//auth
        $ser_optype = trim(input("request.ser_optype"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }

        if ($ser_stats == '0' || $ser_stats == '1') {
            $where['stats'] = $ser_stats;
        }
        if ($ser_optype == '0' || $ser_optype > 0) {
            $where['optype'] = $ser_optype;
        }
        if ($ser_position == '0' || $ser_position > 0) {
            $where['position'] = $ser_position;
        }

        $where['is_delete'] = '0';
        $lists = $this->_model->lists($where, $this->pagesize);
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');
        $lists['data'] = $this->_mapper->listMapper($lists['data']);
        $this->assign('lists', $lists);

        $this->assign('request', input("request."));
        return view('lists');
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('is_delete' => 1), $id);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }

    public function addData()
    {
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $content = trim(input("request.conts"));//auth
            $data2 = array(
                'uid' => $this->LoginUserInfo['UserInfo']['id'],
                'ctime' => date("Y-m-d H:i:s")
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->add($data);
            if ($res) {
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }
        $title = "添加";
        $this->assign('title', $title);
        return view('addData');
    }

    public function infoData()
    {

        $id = trim(input("request.id"));//auth

        $info = $this->_model->getDataForWhere(array('id' => $id));

        $info['UserInfo'] = array();
        if ($info['uid']) {
            $info['UserInfo'] = getUserInfoForID($info['uid']);
        }

        $this->assign('info', $info);
        $title = "详情";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('infoData');
    }

    public function editData()
    {
        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $w = array(
                'id' => $id
            );
            $data2 = array(
                'utime' => date("Y-m-d H:i:s")
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->updateDataForWhere($data, $w);

            if ($res || ($res === 0)) {

                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }

        $this->assign('info', $info);
        //设置全局信息
        $title = "编辑";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');

    }
}